////////////////////////////////////////////////
// DESCRIPTION:
//    Sample SPI Device driver
//
// Legal Notices:
//   Copyright (c) 2008, Telliam Consulting, LLC.
//   All rights reserved.
//
//   Redistribution and use in source and binary forms, with or without modification,
//   are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice, this list
//     of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice, this
//     list of conditions and the following disclaimer in the documentation and/or other
//     materials provided with the distribution.
//   * Neither the name of Telliam Consulting nor the names of its contributors may be
//     used to endorse or promote products derived from this software without specific
//     prior written permission.
//
//   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
//   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
//   SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
//   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
//   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
//   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
//   DAMAGE. 
//
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace FusionWare.SPOT.Hardware
{
    /// <summary>Managed Driver for the emulated Simple SPIDevice</summary>
    /// <remarks>
    /// This demonstrates a managed code driver for an SPI Device. The device
    /// is the emulated FusionWare.Emulator.SimpleSPIDevice. It has only 
    /// one 16 bit "Chip ID" register (or command/response if you like). 
    /// </remarks>
    partial class SimpleSPIDevice : SPIDeviceDriver
    {
        /// <summary>Creates a new SimpleSSPIDevice instance</summary>
        /// <param name="Bus">SPI bus module Id</param>
        /// <param name="ChipSelect">GPIO Pin for the Chip Select</param>
        /// <param name="ChipSelectActiveState">Active state of the chip select for this device</param>
        /// <remarks>
        /// Creates a new SimpleSPIDevice driver instance.
        /// For maximum isolation and encapsulation the application code only needs to
        /// provide the SPI bus and chip select pin information. The rest of the
        /// timing and clock information is specifc to the actual device capabilities
        /// and therefore provided internally in the managed driver rather than being
        /// supplied externally.
        /// </remarks>
        public SimpleSPIDevice( SPI.SPI_module Bus, Cpu.Pin ChipSelectPin, bool ChipSelectActiveState )
        {
            SPI.Configuration cfg = new SPI.Configuration( ChipSelectPin
                                                         , ChipSelectActiveState
                                                         , 1
                                                         , 1
                                                         , false
                                                         , true
                                                         , 16000
                                                         , Bus
                                                         );
            base.Config = cfg;
        }

        /// <summary>Gets the Chip ID</summary>
        /// <value>Chip ID retrieved from the device</value>
        /// <remarks>
        /// This property will cache the ID so that it is actually read from the
        /// device only once. 
        /// </remarks>
        public ChipIDReg ChipID
        {
            get
            {
                if( this._ChipID.Value == 0 )
                    this._ChipID.Value = base.ReadReg16( (ushort)Registers.ChipID );

                return this._ChipID;
            }
        }
        private ChipIDReg _ChipID;
    }
}
